<?php
namespace Home\Controller;
use Think\Controller;
use Home\Model\woZanPay;
use Exception;
class PayController extends BaseController {
    public function pay(){
        $channel_id = session('channel_id');
        $openId = session('openid');
        $player_id = session('user_id');
        if (empty($openId)) {exit('error');}
        
        $body = I('paytitle');
        $total_fee = I('pay'); // 支付金额，单位为分
        $game_id = I('_gameID');
        $gparams = $_POST['gparams'];
        $url = I('post.url');
        $url = str_replace('&amp;','&',$url);
        $url .='&open_id='.$openId;
        if (empty($body) || !is_numeric($total_fee) || !is_numeric($game_id)) {exit('error');}
        try{
            $tmp = json_decode($gparams, true);
            if (!is_array($tmp)) {exit('error');}
        }catch (Exception $exception) {
            exit('error');
        }
        $time = microtime();
        $trade_no = $channel_id.date("YmdHis").substr($time,2,6);// 商户订单号，生成规则其实随意，唯一就行，
        $trade_type = I('payType');

        try{
            $gmap = array('id'=>$game_id);
            $game_separate = M('game')->where($gmap)->getField('separate');
            $game_separate = empty($game_separate)? '0' : $game_separate;

            $cmap = array('id'=>$channel_id);
            $channel_separate = M('channel')->where($cmap)->getField('separate');
            $channel_separate = empty($channel_separate)? '0' : $channel_separate;

            $tmap = array('status'=>'1');
            $toll = M('toll')->where($tmap)->getField('toll');
            $toll = empty($toll)? '0' : $toll;

            $map = array('channel_id'=>$channel_id, 'type'=>3, 'open'=>1);  // 这里直接写死为3
            $kou = M('channel_kou')->where($map)->find();
            if(empty($kou)){
                $pay_show = $total_fee;
            }else{
                $day = date('Y-m-d 00:00:00');
                $map = array('channel_id'=>$channel_id, 'type'=>3, 'day'=>$day);
                $one =M('channel_data')->where($map)->find();
                if (empty($one)) { // 插入
                    $pay_show = getKouStep(0,0,$kou['max_v'],$kou['bi'],$total_fee);
                } else { // 更新
                    $pay_show = getKouStep($one['value'], $one['show_value'], $kou['max_v'],$kou['bi'],$total_fee);
                }
            }
            
            $wozan = new woZanPay;
            $wozan->setValue('amount',$total_fee);
            $wozan->setValue('productNo',$wozan->product_no);
            $wozan->setValue('orderNo',$trade_no);
            $wozan->setValue('payType',$trade_type);
            $wozan->setValue('returnurl',$url);
            $wozan->setExt('clientIP',get_client_ip());

            $data = array(
                'trade_no'=>$trade_no,
                'player_id'=>$player_id,
                'pay'=>$total_fee,
                'game_id'=>$game_id,
                'callback_parms' =>$gparams,
                'game_separate'=>$game_separate,
                'channel_separate'=>$channel_separate,
                'toll'=>$toll,
                'pay_show'=>$pay_show
            );


            $bool = M('trade')->add($data);
            if(!$bool){
                exit('网络错误');
            }
            $res = json_decode($wozan->order(),true);
            //$res = $wozan->order();
            $url = $res['url'];
            //$this->ajaxReturn($res);
            if($url){
                $this->ajaxReturn(array('status'=>true,'url'=>$url,'trade_no'=>$trade_no));
            }else{
                $this->ajaxReturn(array('status'=>false,'a'=>$res));
            }
        } catch (Exception $exception) {
            exit($exception->getMessage());
        }
        

    }

    public function pay_status(){
        $trade_no = I('trade_no');
        $status = M('trade')->where('trade_no = "'.$trade_no.'"')->getField('pay_status');
        if($status){
            $this->ajaxReturn(array('status'=>true));
        }else{
            $this->ajaxReturn(array('status'=>false));
        }
    }
}